#-*- coding: utf-8 -*-

import random

def binaryRandom(p):
	if random.random() < p:
		return 1
	else:
		return 0


def initialProbabilityVector(length):
	p = []
	for i in range(length):
		p.append(0.5)
	return p


def randomIndividual(pbb):
	x = []
	for i in range(len(pbb)):
		x.append(binaryRandom(pbb[i]))
	return x


def randomPopulation(pbb, size):
	x = []
	for i in range(size):
		x.append(randomIndividual(pbb))
	return x

def bestIndividual(population, function):
	best = population[0]
	bestVal = function(population[0])
	for i in range(1, len(population)):
		val = function(population[i])
		if val > bestVal:
			bestVal = val
			best = population[i]

	return best

def populationEval(population, fitFun):
	best = population[0]
	bestVal = fitFun(population[0])
	res = [[population[0], bestVal]]

	for i in range(1, len(population)):
		val = fitFun(population[i])
		res.append([population[i], val])
		if val > bestVal:
			bestVal = val
			best = population[i]

	return (best, bestVal, res)

def cross(*lists):
    """ iloczyn wektorowy """
    result = [[]]
    for oneList in lists:
        result = [elems+[elem] 
                    for elems in result 
                    for elem in oneList]
    return result

